library(geneviewer)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.2     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(webshot2)
a <- read_tsv("/Volumes/lab_kingsley/ambenj/myosin_dups/analysis/assemblies/figure/combined_annotations.txt") %>% 
  mutate(start = case_when(orientation == "reverse" ~ max,
                           TRUE ~ min),
         end = case_when(orientation == "reverse" ~ min,
                         TRUE ~ max)) %>% 
  arrange(allele_type, min)
## Rows: 39 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (5): gene, gene_labels, allele_type, sequence, orientation
## dbl (5): original_min, original_max, length, min, max
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
a
wong_palette <- c("#000000", "#e69f00", "#56b4e9", "#009e73", "#f0e442", "#0072b2", "#d55e00", "#cc79a7")
custom_colors <- c("#d9d9d9", "#e69f00", "#f0e442", "#009e73","#d9d9d9", "#d9d9d9", "#d9d9d9", "#d9d9d9","#d9d9d9","#d9d9d9", "#d9d9d9", "#d9d9d9")
#new red#d73027

cluster_plot <- a %>% 
#  filter(!str_detect(gene, "nlrc5|LOC")) %>% 
GC_chart(start = "start", 
         end = "end", 
         group = "gene", 
         cluster = "allele_type", 
         width = "550px", 
         height = "250px") %>% 
 GC_align(
     id_column = "gene", 
     id = "MYH3C1",
     align = "left" # center / right
     ) %>% 
  GC_annotation(
    cluster = 1, type = "rectangle", rotation = 0,
    position = list(
        list(c(61754, 10), c(80331, 90))),
    style = list(stroke ="black", strokeWidth = 1)) %>% 
  GC_annotation(
    cluster = 2, type = "rectangle", rotation = 0,
    position = list(
        list(c(64361, 10), c(81965, 90)),
        list(c(81966, 10), c(99605, 90))),
    style = list(stroke ="black", strokeWidth = 1)) %>% 
  GC_annotation(
    cluster = 3, type = "rectangle", rotation = 0,
    position = list(
        list(c(64308, 10), c(81907, 90)),
        list(c(81908, 10), c(99526, 90)),
        list(c(99527, 10), c(117150, 90))),
    style = list(stroke ="black", strokeWidth = 1)) %>% 
  GC_annotation(
    cluster = 4, type = "rectangle", rotation = 0,
    position = list(
        list(c(63375, 10), c(80978, 90)),
        list(c(80979, 10), c(98601, 90)),
        list(c(98602, 10), c(116227, 90)),
        list(c(116228, 10), c(133886, 90))),
    style = list(stroke ="black", strokeWidth = 1)) %>% 
#  GC_genes(marker = "boxarrow", marker_size = "small") %>%
  GC_scale(axis_type = "range", hidden=TRUE) %>% 
  GC_scaleBar(title = "20kb", scaleBarUnit = 20000, cluster=5)  %>%
  GC_labels("gene_labels", fontSize=9, adjustLabels = TRUE, y = 6) %>%
  GC_clusterLabel(title = unique(a$allele_type)) %>%
  GC_legend(FALSE) %>% 
  GC_color(customColors = custom_colors)

cluster_plot
?GC_annotation
# Save cluster plot to temp.html file
htmlwidgets::saveWidget(cluster_plot, "temp.html", selfcontained = TRUE)

# Save plot to .png (or .jpg, .jpeg, .webp, .pdf)
webshot2::webshot(
  "temp.html", 
  "/Volumes/lab_kingsley/ambenj/myosin_dups/analysis/assemblies/figure/cluster_plot.pdf", 
  zoom = 10, # Increase zoom for higher resolution
  selector = ".geneviewer")
## file:////Volumes/lab_kingsley/ambenj/myosin_dups/scripts/R/temp.html screenshot completed